goog.provide('dkpoints.rpc.commands.LogoutCommand');

goog.require('dkpoints.rpc.commands.AuthCheckResponse');
goog.require('dkpoints.rpc.commands.Command');



/**
 * Command object for logging out via RPC
 * @param {function(dkpoints.rpc.commands.AuthCheckResponse)} callersOnSuccess
 *    The callback to accept the new auth status after logout is complete.
 * @param {function(goog.net.XhrManager.Event)} callersOnFailure
 *    The callback to accept the XHR response event if the request fails.
 * @constructor
 * @extends {dkpoints.rpc.commands.Command}
 */
dkpoints.rpc.commands.LogoutCommand = function(callersOnSuccess,
                                               callersOnFailure) {
  var commandID = 'Logout:' + (new goog.date.DateTime()).toUTCIsoString();
  var url = '/apis/auth/logout/' + commandID;

  dkpoints.rpc.commands.Command.call(this,
                                     callersOnSuccess, callersOnFailure,
                                     commandID, url, 'GET');
};
goog.inherits(dkpoints.rpc.commands.LogoutCommand,
              dkpoints.rpc.commands.Command);


/**
 * The callback called by the RPC to process a successful logout request.
 * @param {goog.net.XhrManager.Event} event The Complete event from the XHR.
 */
dkpoints.rpc.commands.LogoutCommand.prototype.onSuccess = function(event) {
  var target = event.target;

  // The live environment doesn't return a proper response for some reason,
  // so check if the response has actual text in it.
  if (event.target.getResponseText() === '') {
    this.onFailure(event);
  }
  else {
    var response = event.target.getResponseJson(),
        authStatus = new dkpoints.rpc.commands.AuthCheckResponse(response);
    this.callersOnSuccess_(authStatus);
  }
};


/**
 * The callback called by the RPC to process a failed logout request.
 * @param {goog.net.XhrManager.Event} event The Complete event from the XHR.
 */
dkpoints.rpc.commands.LogoutCommand.prototype.onFailure = function(event) {
  this.callersOnFailure_(event);
};
